package com.jincp.filepicker.ui.image;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;

import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.BitmapUtil;
import com.jincp.common.utils.ToastUtil;
import com.jincp.filepicker.R;
import com.jincp.filepicker.base.FileBaseActivity;
import com.jincp.filepicker.bean.ImageFile;
import com.jincp.filepicker.widgets.CropImageView;

import java.io.File;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: ImageCropActivity
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/23 14:29
 * =========================================================
 */
public class ImageCropActivity extends FileBaseActivity implements BitmapUtil.BitmapSaveCompleteListener {


    public static final String IMAGE_FILE = "IMAGE_FILE";

    private ImageFile imageFile;

    private Button btnComplete;
    private CropImageView cropImageView;
    private Bitmap bitmap;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            imageFile = intent.getParcelableExtra(IMAGE_FILE);
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_image_crop;
    }

    @Override
    protected void initViews() {
        setCustomToolBar((Toolbar) findViewById(R.id.toolbar), new ToolBarOptions());

        //初始化View
        btnComplete = (Button) findViewById(R.id.btn_complete);
        btnComplete.setText(getString(R.string.complete));
        btnComplete.setOnClickListener(this);
        btnComplete.setEnabled(true);

        cropImageView = (CropImageView) findViewById(R.id.cv_crop_image);
        cropImageView.setFocusStyle(ImagePickerManager.getInstance().getStyle());
        cropImageView.setFocusWidth(ImagePickerManager.getInstance().getFocusWidth());
        cropImageView.setFocusHeight(ImagePickerManager.getInstance().getFocusHeight());

        //缩放图片
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imageFile.getPath(), options);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        options.inSampleSize = calculateInSampleSize(options, displayMetrics.widthPixels, displayMetrics.heightPixels);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(imageFile.getPath(), options);
        cropImageView.setImageBitmap(bitmap);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int width = options.outWidth;
        int height = options.outHeight;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = width / reqWidth;
            } else {
                inSampleSize = height / reqHeight;
            }
        }
        return inSampleSize;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_complete) {
            Bitmap bitmap = cropImageView.getCropBitmap(ImagePickerManager.getInstance().getOutPutX(), ImagePickerManager.getInstance().getOutPutY());
            if (bitmap == null){
                ToastUtil.getInstance().show(R.string.create_file_failure);
            } else {
                Bitmap.CompressFormat format;
                if (ImagePickerManager.getInstance().getStyle() == CropImageView.Style.CIRCLE) {
                    ImagePickerManager.getInstance().createFile("CROP_", ".png");
                    format = Bitmap.CompressFormat.PNG;
                } else {
                    ImagePickerManager.getInstance().createFile("CROP_", ".jpg");
                    format = Bitmap.CompressFormat.JPEG;
                }
                BitmapUtil.saveBmpToFile(bitmap, ImagePickerManager.getInstance().getTakeImageFile(), format, this);
                btnComplete.setEnabled(false);
            }
        }
    }

    @Override
    public void onBitmapSaveSuccess(File file) {
        //裁剪后替换掉返回数据的内容，但是不要改变全局中的选中数据
        ImageFile image = new ImageFile();
        image.setPath(file.getAbsolutePath());

        Intent intent = new Intent();
        intent.putExtra(ImagePickerManager.EXTRA_RESULT_ITEM, image);
        setResult(ImagePickerManager.RESULT_IMAGE_ITEM, intent);
        finish();
    }

    @Override
    public void onBitmapSaveError() {
        btnComplete.setEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != bitmap && !bitmap.isRecycled()) {
            bitmap.recycle();
            bitmap = null;
        }
    }
}
